//
//  DYJScrollView.m
//  FunWay
//
//  Created by dllo on 16/5/13.
//  Copyright © 2016年 dyj. All rights reserved.
//

#import "DYJScrollView.h"
#import "DYJScrollViewCell.h"
#import "DYJScrollViewModel.h"


@interface DYJScrollView ()
@property (nonatomic, retain) UICollectionView *collectionView;
//pageControl
@property (nonatomic, retain)UIPageControl *headerPageControl;

@property (nonatomic, retain)NSTimer *timer;

@end
@implementation DYJScrollView
- (void)dealloc
{
    self.collectionView.delegate = nil;
    self.collectionView.dataSource = nil;
}
- (void)setScrollArray:(NSMutableArray *)scrollArray
{
    if (_scrollArray != scrollArray) {
        _scrollArray = scrollArray;
        [_timer invalidate];
        _timer = nil;

        if (_scrollArray.count != 0) {
            
//            [_scrollArray insertObject:[_scrollArray lastObject] atIndex:0];
//            [_scrollArray addObject:[_scrollArray objectAtIndex:1]];
            self.collectionView.contentOffset = CGPointMake(self.frame.size.width, 0);
            //pageControl
            self.headerPageControl.numberOfPages = _scrollArray.count - 2;
            
            self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }
        [self.collectionView reloadData];
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //轮播collecotionView布局
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        //初始化轮播collecotionView
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.pagingEnabled = YES;
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        [self addSubview:_collectionView];
        
        //注册collectionCell
        [self.collectionView registerClass:[DYJScrollViewCell class] forCellWithReuseIdentifier:@"headerCell"];

        [self.collectionView reloadData];
        
        self.headerPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100 * FitWidth, self.frame.size.height - 20, 175 * FitWidth, 20)];
        
        [self addSubview:_headerPageControl];
    }
    return self;
}
-(void)timerAction
{
    self.headerPageControl.currentPage = [self.collectionView indexPathsForVisibleItems].lastObject.item; 
    NSInteger nextIndex = [self.collectionView indexPathsForVisibleItems].lastObject.item + 1;
    //滚到最后一张
    if (nextIndex == _scrollArray.count - 1) {
        self.headerPageControl.currentPage = 0;
        nextIndex = 1;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    }
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (nextIndex == _scrollArray.count) {
        nextIndex = 1;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    }
    //滚到下一张
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
//----------------------collectionView------------------------//
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _scrollArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DYJScrollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headerCell" forIndexPath:indexPath];
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (_scrollArray.count != 0) {
        DYJScrollViewModel *adModel = [_scrollArray objectAtIndex:indexPath.item];
        cell.model = adModel;
    }
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DYJScrollViewModel *adModel = [_scrollArray objectAtIndex:indexPath.item];
    [self.delegate pushTapScrollViewControllerById:adModel.id ColorBy:indexPath.item Tilte:adModel.title];
}
//滑动开始方法
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [_timer invalidate];
    _timer = nil;
    // [_timer setFireDate:[NSDate distantFuture]];
}
//滑动结束方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //[_timer setFireDate:[NSDate distantFuture]];
    //滑到最后
    if ([self.collectionView indexPathsForVisibleItems].lastObject.item == _scrollArray.count - 1) {
        self.headerPageControl.currentPage = 0;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    }
    //滑到第一页
    else if([self.collectionView indexPathsForVisibleItems].lastObject.item == 0)
    {
        self.headerPageControl.currentPage = _scrollArray.count - 3;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_scrollArray.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    }
    else
    {
    self.headerPageControl.currentPage = [self.collectionView indexPathsForVisibleItems].lastObject.item - 1;
    }
    //开启定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
   // [_timer setFireDate:[NSDate distantPast]];
}
@end
